import connectMongoDb from "@/libs/mongodb";
import Topic from "@/models/topic";
import { NextResponse } from "next/server";

/**
 * http get
 * http://localhost:3000/api/topics/[id]
 */
export async function GET(request: any, { params }: any) {
    const { id } = params;
    await connectMongoDb();
    const topic = await Topic.findOne({ _id: id });
    return NextResponse.json({ topic }, { status: 200 });
}

/**
 * http put
 * http://localhost:3000/api/topics/[id]
 */
export async function PUT(request: any, { params }: any) {
    const { id } = params;
    //从request body 中解析参数newTitle, newDescription 给title, description 赋值
    const { newTitle: title, newDescription: description } = await request.json();
    await connectMongoDb();
    await Topic.findByIdAndUpdate(id, { title, description });
    return NextResponse.json({ message: "Topic Updated" }, { status: 200 });
}